package util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.joda.time.DateTime;

public class DateTimeUtil {

	public static Date parseDateTime(String dateTime) {
		Date res = null;
		if (dateTime != null && !dateTime.isEmpty()) {
			res = parseDate(dateTime, ConfigUtil.visualDateTimeFormat());
		}
		return res;
	}

	public static Date parseDateTime(String date, String time) {
		Date res = null;
		if (date != null && !date.isEmpty()) {
			if (time == null || time.isEmpty()) {
				res = parseDate(date, ConfigUtil.dateFormat());
			} else {
				res = parseDate(date + " " + time, ConfigUtil.dateTimeFormat());
			}
		}
		return res;
	}

	public static Date parseDate(String strDate, String strFormat) {
		Date result = null;
		try {
			SimpleDateFormat formater = new SimpleDateFormat(strFormat);
			result = formater.parse(strDate);
		} catch (Exception ex) {

		}
		return result;
	}

	public static Date parseDate(String strDate) {
		Date result = null;
		try {
			SimpleDateFormat formater = new SimpleDateFormat(ConfigUtil.dateFormat());
			result = formater.parse(strDate);
		} catch (Exception ex) {

		}
		return result;
	}

	public static String getDayString(Date date) {
		String strResult = "";
		if (date != null) {
			SimpleDateFormat formater = new SimpleDateFormat(ConfigUtil.dateFormat());
			strResult = formater.format(date);
		}
		return strResult;
	}

	public static String getDateTimeString(Date date) {
		String strResult = "";
		if (date != null) {
			SimpleDateFormat formater = new SimpleDateFormat(ConfigUtil.dateTimeFormat());
			strResult = formater.format(date);
		}
		return strResult;
	}
	
	public static String getTimeString(Date date) {
		String strResult = "";
		if (date != null) {
			SimpleDateFormat formater = new SimpleDateFormat(ConfigUtil.timeFormat());
			strResult = formater.format(date);
		}
		return strResult;
	}

	public static Date getDayWithoutTime(Date ori) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(ori);

		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);

		Date res = calendar.getTime();
		return res;
	}

	public static DateTime getDayWithoutTime(DateTime ori) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, ori.getYear());
		calendar.set(Calendar.MONTH, ori.getMonthOfYear());
		calendar.set(Calendar.DAY_OF_MONTH, ori.getDayOfMonth());

		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);

		Date date = calendar.getTime();

		DateTime res = new DateTime(date);
		return res;
	}

	public static DateTime cloneDateTime(DateTime ori) {
		Date date = ori.toDate();
		DateTime res = new DateTime(date);
		return res;
	}

	public static boolean isToday(DateTime date) {
		Calendar today = Calendar.getInstance();

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date.toDate());

		boolean res = calendar.get(Calendar.YEAR) == today.get(Calendar.YEAR) && calendar.get(Calendar.MONTH) == today.get(Calendar.MONTH) && calendar.get(Calendar.DAY_OF_MONTH) == today.get(Calendar.DAY_OF_MONTH);

		return res;
	}

	public static boolean inDayRange(DateTime day, DateTime startDay, DateTime endDay) {
		boolean res = !isBeforeDay(day, startDay) && !isAfterDay(day, endDay);
		return res;
	}

	public static boolean isBeforeDay(DateTime date, DateTime dateToCompare) {
		boolean res = getDayWithoutTime(date).isBefore(getDayWithoutTime(dateToCompare));
		return res;
	}

	public static boolean isAfterDay(DateTime date, DateTime dateToCompare) {
		boolean res = getDayWithoutTime(date).isAfter(getDayWithoutTime(dateToCompare));
		return res;
	}

	public static List<MonthInfo> getMonthInRange(DateTime firstDay, DateTime lastDay) {
		List<MonthInfo> months = new ArrayList<MonthInfo>();

		if (!DateTimeUtil.isAfterDay(firstDay, lastDay)) {

			MonthInfo curMonth = null;
			DateTime date = firstDay;

			while (!DateTimeUtil.isAfterDay(date, lastDay)) {

				if (curMonth == null) {
					// first day of month
					curMonth = new MonthInfo();
					curMonth.setFirstDay(DateTimeUtil.cloneDateTime(date));
					curMonth.setLastDay(DateTimeUtil.cloneDateTime(date));
					curMonth.setNoOfDay(1);
				}

				date = date.plusDays(1);

				if (date.getMonthOfYear() != curMonth.getLastDay().getMonthOfYear() || DateTimeUtil.isAfterDay(date, lastDay)) {
					// go to next month

					MonthInfo month = new MonthInfo();
					month.setFirstDay(curMonth.getFirstDay());
					month.setLastDay(curMonth.getLastDay());
					month.setNoOfDay(curMonth.getNoOfDay());

					months.add(month);

					System.out.println(month.getFirstDay());
					System.out.println(month.getLastDay());

					curMonth = null;

				} else {
					curMonth.setLastDay(DateTimeUtil.cloneDateTime(date));
					curMonth.setNoOfDay(curMonth.getNoOfDay() + 1);
				}
			}
		}

		return months;
	}

}
